package com.zx.viewlearn.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zx.viewlearn.R;

import java.util.ArrayList;
import java.util.List;

public class ViewLearnActivity19PullToRefresh extends AppCompatActivity {
    RecyclerView content_rv;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    final List<String> list = new ArrayList<>();

    class MYViewHolder extends RecyclerView.ViewHolder {
        View root;
        TextView name_tv;

        public MYViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            name_tv = itemView.findViewById(R.id.name_tv);
        }

        public void setHeight() {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(root.getLayoutParams());
            lp.height = (int) Math.max(300, (Math.random() * 600));
            root.setLayoutParams(lp);

        }

        public void setName(String name) {
            if (name_tv != null) name_tv.setText(name);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_learn_activity19_pull_to_refresh);
        content_rv = findViewById(R.id.content_rv);
        adapter = new RecyclerView.Adapter<ViewLearnActivity19PullToRefresh.MYViewHolder>() {

            @Override
            public ViewLearnActivity19PullToRefresh.MYViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//                View view = getLayoutInflater().inflate(R.layout.item_19, null);
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_19, parent,
                        false);
                ViewLearnActivity19PullToRefresh.MYViewHolder mViewHolder = new ViewLearnActivity19PullToRefresh.MYViewHolder(view);
                return mViewHolder;
            }

            @Override
            public void onBindViewHolder(ViewLearnActivity19PullToRefresh.MYViewHolder holder, int position) {
                holder.setName(list.get(position));
                holder.setHeight();
            }

            @Override
            public int getItemCount() {
                return list.size();
            }
        };
        layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        ((StaggeredGridLayoutManager) layoutManager).setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);//不设置的话，图片闪烁错位，有可能有整列错位的情况。
        content_rv.setLayoutManager(layoutManager);
        content_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                ((StaggeredGridLayoutManager) layoutManager).invalidateSpanAssignments(); //防止第一行到顶部有空白区域
            }
        });
        for (int i = 0; i < 60; i++) {
            list.add("name---" + i);
        }
        content_rv.setAdapter(adapter);
    }

    public void ViewLearnActivity19PullToRefresh_2(View view) {
        Intent intent = new Intent(this, ViewLearnActivity19PullToRefresh_2.class);
        startActivity(intent);
    }
}
